<?php
class BasePayment extends Object{

    var $_gateway   = '';
    /* 支付方式唯一标识 */
    var $_code      = '';
    var $_pid       = '';
    /* 构造函数 */
    function __construct($payment_info = array()){
        $this->_info   = $payment_info;
        $this->_pid     = $payment_info['payment_id'];
        $this->_config = unserialize($payment_info['config']);
    }
    /* 获取支付表单 */
    function get_payform(){
        return $this->_create_payform('POST');
    }
    /* 获取规范的支付表单数据 */
    function _create_payform($method = '', $params = array()){
        return array(
            'online'    =>  $this->_info['is_online'],
            'desc'      =>  $this->_info['payment_desc'],
            'method'    =>  $method,
            'gateway'   =>  $this->_gateway,
            'params'    =>  $params,
        );
    }
    /* 获取通知地址 */
    function _create_notify_url($order_sn_main,$type){
        if(!$order_sn_main || !$type){
            $this->_error('miss order_sn_main or payment_type');
            exit;
        }
        return "http://pay.ppwh.com.cn/index.php?c=paycallback&a={$type}&main={$order_sn_main}";
    }
    /* 获取返回地址 */
    function _create_return_url($order_sn_main,$type){
        if(!$order_sn_main || !$type){
            $this->_error('miss order_sn_main or payment_type');
            exit;
        }
        return "http://pay.ppwh.com.cn/index.php?c=paycallback&a={$type}&main={$order_sn_main}";
    }
    /* 获取外部交易号 */
    protected function _get_trade_sn($order_sn_main,$money){
        $out_order_sn = $this->_make_trade_sn();
        $is_exist = modcall("paysign","get",array("out_order_sn={$out_order_sn}"));
        if(!empty($is_exist)){//重复了
            return $this->_get_trade_sn($order_sn_main,$money);
        }
        
        $data = array(
            'order_sn_main'=>$order_sn_main,
            'out_order_sn'=>$out_order_sn,
            'pay_id' => $this->_pid,
            'money' => $money,
            'ctime' => gmtime(),
            'status'=> 'ready'
        );

        $id=modcall("paysign","add",array($data));
        if($id){
            return $out_order_sn;
        }
    }
    /* 生成外部交易号 */
    protected function _make_trade_sn(){
        /* 选择一个随机的方案 */
        $timestamp = gmtime();
        $ym = date('ym', $timestamp);
        $out_trade_sn = $ym . str_pad(mt_rand(1,999999),6, '0', STR_PAD_LEFT);
        return $out_trade_sn;
    }
    /* 获取商品简介 */
    function _get_subject($order_sn_main){
        return '中国·品牌商城订单 : ' . $order_sn_main;
    }
    /* 将验证结果反馈给网关 */
    function verify_result($result){
        if ($result){
            echo 'success';
        }else{
            echo 'fail';
        }
    }
}


class Object
{
    var $_errors = array();
    var $_errnum = 0;
    function __construct()
    {
        $this->Object();
    }
    function Object()
    {
        #TODO
    }
    /**
     *    触发错误
     *
     *    @author    Garbin
     *    @param     string $errmsg
     *    @return    void
     */
    function _error($msg, $obj = '')
    {
        if(is_array($msg))
        {
            $this->_errors = array_merge($this->_errors, $msg);
            $this->_errnum += count($msg);
        }
        else
        {
            $this->_errors[] = compact('msg', 'obj');
            $this->_errnum++;
        }
    }

    /**
     *    检查是否存在错误
     *
     *    @author    Garbin
     *    @return    int
     */
    function has_error()
    {
        return $this->_errnum;
    }

    /**
     *    获取错误列表
     *
     *    @author    Garbin
     *    @return    array
     */
    function get_error()
    {
        return $this->_errors;
    }
}
?>